package main

import (
	"flag"
	"fmt"
	"github.com/nats-io/nats.go"
	"github.com/zeromicro/go-zero/core/conf"
	"main/config"
	"main/socket"
	"main/svc"
	"net/http"
)

func main() {
	configPath := "etc/main.yaml"

	var configFile = flag.String("f", configPath, "the config file")

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)
	socket.Setup(ctx)
	ctx.NC.Subscribe(ctx.Config.SubRedisKey, func(msg *nats.Msg) {
		fmt.Println("接受到消息了")
		err := socket.ProcessMessages(msg.Data)
		fmt.Println(err)
	})

	http.HandleFunc("/ws", socket.HandleWebSocket)
	fmt.Println("WebSocket 服务启动，监听地址：http://localhost:" + c.Prod + "/ws")
	err := http.ListenAndServe(":"+c.Prod, nil)
	if err != nil {
		fmt.Println("启动服务失败：", err)
	}

}
